package org.zachary.commonservice.entity;

import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

@Data
public class Result<T>  implements Serializable {
    @Serial
    private static final long serialVersionUID = -3960261604605958516L;

    private ServiceResultStatus status;
    private String message;
    private T data;
    private Exception exception;

    /**
     * 1. status  message  data exception
     * 2. status message data
     * 3. status message
     * 4. status exception
     * 5. status message exception
     */
    // 1. status  message  data exception
    private Result(ServiceResultStatus status, String message, T data, Exception exception) {
        this.status = status;
        this.message = message;
        this.data = data;
        this.exception = exception;
    }
    // 2. status message data
    private Result(ServiceResultStatus status, String message,T data) {
        this(status, message, data, null);
    }
    // 3. status message
    private Result(ServiceResultStatus status, String message) {
        this(status, message, null,null);
    }
    // 4. status exception
    private Result(ServiceResultStatus status,Exception e) {
        this(status, e.getMessage(), null,e);
    }
    // 5. status message exception
    private Result(ServiceResultStatus status,String message,Exception e) {
        this(status, message,null,e);
    }
    // 6.

    public static <T> Result<T> success() {
        return new Result<>(ServiceResultStatus.SUCCESS, "成功");
    }
    public static <T> Result<T> success(T data) {
        return new Result<>(ServiceResultStatus.SUCCESS,"成功",data);
    }
    public static <T> Result<T> success(String message) {
        return new Result<T>(ServiceResultStatus.SUCCESS,message);
    }
    public static <T> Result<T> success(String message,T data) {
        return new Result<T>(ServiceResultStatus.SUCCESS,message,data);
    }
    public static <T> Result<T> fail() {
        return new Result<T>(ServiceResultStatus.FAILURE,"失败");
    }
    public static <T> Result<T> fail(String message) {
        return new Result<T>(ServiceResultStatus.FAILURE,message);
    }
    public static <T> Result<T> exception(Exception e) {
        return new Result<T>(ServiceResultStatus.EXCEPTION,e);
    }
    public static <T> Result<T> exception(String message) {
        return new Result<T>(ServiceResultStatus.EXCEPTION,message);
    }
    public static <T> Result<T> exception(String message,Exception e) {
        return new Result<T>(ServiceResultStatus.EXCEPTION,message,e);
    }
    public static <T> Result<T> InvalidParameter(){
        return new Result<>(ServiceResultStatus.InvalidParameter,"无效的参数");
    }
    public static <T> Result<T> InvalidParameter(String message){
        return new Result<T>(ServiceResultStatus.InvalidParameter,message);
    }
}
